package com.lz.ovuola.service.assembler;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lz.ovuola.entity.PhysiologicalDataDo;
import com.lz.ovuola.service.dto.PhysiologicalDataDto;
import com.lz.ovuola.service.dto.PhysiologicalDataListProDto;
import com.lz.ovuola.service.dto.PhysiologicalDataListProListDto;
import com.lz.ovuola.util.date.DateUtils;
import com.lz.ovuola.util.exception.BusinessException;
import com.lz.ovuola.util.exception.msg.CommonMsgEnum;
import com.lz.ovuola.util.threadlocal.UserContextHolder;

/**
 * 包装工具
 * 
 * @author fz
 *
 */
public class PhysiologicalDataListProListAssembler implements
		Assembler<PhysiologicalDataListProListDto> {

	private Logger logger = LoggerFactory
			.getLogger("PhysiologicalDataListProListAssembler");
	private static final PhysiologicalDataListProListAssembler ASSEMBLER = new PhysiologicalDataListProListAssembler();
	private final PhysiologicalDataListProDto EMPTY = new PhysiologicalDataListProDto();

	private PhysiologicalDataListProListAssembler() {
		super();
	}

	public static PhysiologicalDataListProListAssembler getAssembler() {
		return ASSEMBLER;
	}

	public PhysiologicalDataListProListDto toDto(final Object object,
			final Date startTime, final Date endTime) throws Exception {
		if (object instanceof List) {
			@SuppressWarnings("unchecked")
			List<PhysiologicalDataDo> dos = (List<PhysiologicalDataDo>) object;
			final LocalDate startTimeLocal = DateUtils
					.dateToLocalDate(startTime);
			final LocalDate endTimeLocal = DateUtils.dateToLocalDate(endTime);
			LocalDate nextDate = startTimeLocal;
			final List<PhysiologicalDataListProDto> dataListProDtos = new ArrayList<PhysiologicalDataListProDto>();
			final PhysiologicalDataListProListDto physiologicalDataListProListDto = new PhysiologicalDataListProListDto();
			while (!endTimeLocal.isBefore(nextDate)) {
				PhysiologicalDataListProDto physiologicalDataListProDto = new PhysiologicalDataListProDto();
				List<PhysiologicalDataDto> dtos = new ArrayList<PhysiologicalDataDto>();
				for (PhysiologicalDataDo physiologicalDataDo : dos) {
					if (nextDate.equals(DateUtils
							.dateToLocalDate(physiologicalDataDo
									.getDetectionTime()))) {
						dtos.add(assembler(physiologicalDataDo));
					}
				}
				if (!CollectionUtils.isEmpty(dtos)) {
					physiologicalDataListProDto.setPhysiologicalStage(dtos.get(
							0).getPhysiologicalStage());
					physiologicalDataListProDto.setDtos(dtos);
					dataListProDtos.add(physiologicalDataListProDto);
				} else {
					dataListProDtos.add(new PhysiologicalDataListProDto());
				}
				nextDate = nextDate.plusDays(1);
			}
			physiologicalDataListProListDto.setDtos(dataListProDtos);
			return physiologicalDataListProListDto;
		} else {
			logger.error(
					"---用户id：{},List<PhysiologicalDataDo>转PhysiologicalDataListDto过程中出现异常",
					UserContextHolder.get().getUserId());
			throw new BusinessException(
					CommonMsgEnum.SERVER_INTERNAL_ERROR.getStatus(),
					CommonMsgEnum.SERVER_INTERNAL_ERROR.getMsg());
		}
	}

	private PhysiologicalDataDto assembler(
			PhysiologicalDataDo physiologicalDataDo) {
		PhysiologicalDataDto physiologicalDataDto = new PhysiologicalDataDto();
		physiologicalDataDto.setEstimateNextStartTime(physiologicalDataDo
				.getEstimateNextStartTime());
		physiologicalDataDto.setEstimateOvulationTime(physiologicalDataDo
				.getEstimateOvulationTime());
		physiologicalDataDto
				.setEstimateOvulationTimeDescription(physiologicalDataDo
						.getEstimateOvulationTimeDescription());
		physiologicalDataDto.setFormula(physiologicalDataDo.getFormula());
		physiologicalDataDto.setPhysiologicalStage(physiologicalDataDo
				.getPhysiologicalStage());
		physiologicalDataDto.setPregnancyRateDescription(physiologicalDataDo
				.getPregnancyRateDescription());
		physiologicalDataDto.setWaterContent(physiologicalDataDo
				.getWaterContent());
		physiologicalDataDto.setLeedingGums(physiologicalDataDo
				.getLeedingGums());
		physiologicalDataDto.setSick(physiologicalDataDo.getSick());
		physiologicalDataDto.setDetectionTime(physiologicalDataDo
				.getDetectionTime());
		return physiologicalDataDto;
	}

	@Override
	public PhysiologicalDataListProListDto toDto(Object object)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
